package com.infore.operation.utils;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
import java.util.Date;

/**
 * 日历工具�?
 * @author zwt
 *
 */
public class FormatUtil {
	public static SimpleDateFormat second=new SimpleDateFormat("yyyyMMddHHmmss");
	public static SimpleDateFormat minute=new SimpleDateFormat("yyyyMMddHHmm");
	public static SimpleDateFormat hour=new SimpleDateFormat("yyyyMMddHH");
	public static SimpleDateFormat day=new SimpleDateFormat("yyyyMMdd");
	public static SimpleDateFormat month=new SimpleDateFormat("yyyyMM");
	public static SimpleDateFormat secondG=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat YMDHM=new SimpleDateFormat("yyyy-MM-dd HH:mm");
	public static SimpleDateFormat secondA=new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat secondZ=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
	public static SimpleDateFormat secondE=new SimpleDateFormat("yyyy-MM-dd 23:00:00");
	public static SimpleDateFormat millisecond=new SimpleDateFormat("yyyyMMddHHmmssSSS");
	public static SimpleDateFormat historydate=new SimpleDateFormat("yyyy-MM-dd HH");
	public static SimpleDateFormat historymonth=new SimpleDateFormat("yyyy-MM");
	
	public static SimpleDateFormat daily=new SimpleDateFormat("yyyy��MM��dd��");
	/**
	 * 除法运算
	 * @Title: divided
	 * @Description: TODO
	 * @date 2016�?�?0�?下午4:55:46
	 * @author  周文�?
	 */
	public static Double divided( double dividend, double divisor, int digit ) {
		Double result = 0.0;
		if ( divisor != 0L ) {
			result = dividend / divisor;
		}
		if ( digit < 0 ) {
			digit = 0;
		}
		BigDecimal bd = new BigDecimal(result);
		return bd.setScale(digit, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	
	/**
	 * 获取适当小数位数
	 * @Title: scale
	 * @Description: TODO
	 * @date 2016�?�?1�?下午5:53:58
	 * @author  周文�?
	 */
	public static Double scale(Double value,Integer digit){
		if(digit==null){
			digit=new Integer(4);
		}
		BigDecimal bd = new BigDecimal(value);
		return bd.setScale(digit, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	 public static List<String> getMonthBetween(String minDate, String maxDate) throws Exception {  
	        ArrayList<String> result = new ArrayList<String>();  
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");//格式化为年月  
	  
	        Calendar min = Calendar.getInstance();  
	        Calendar max = Calendar.getInstance();  
	  
	        min.setTime(sdf.parse(minDate));  
	        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);  
	  
	        max.setTime(sdf.parse(maxDate));  
	        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);  
	  
	        Calendar curr = min;  
	        while (curr.before(max)) {  
	         result.add(sdf.format(curr.getTime()));  
	         curr.add(Calendar.MONTH, 1);  
	        }  
	  
	        return result;  
	    }
	public static String string2MD5(String inStr){
		MessageDigest md5 = null;
		try{
			md5 = MessageDigest.getInstance("MD5");
		}catch (Exception e){
			System.out.println(e.toString());
			e.printStackTrace();
			return "";
		}
		char[] charArray = inStr.toCharArray();
		byte[] byteArray = new byte[charArray.length];

		for (int i = 0; i < charArray.length; i++)
			byteArray[i] = (byte) charArray[i];
		byte[] md5Bytes = md5.digest(byteArray);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++){
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16)
				hexValue.append("0");
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();

	}
//	public static void main(String[] args) {
//		System.out.println(divided(9,10,2));
//	}
//    public static String windDireTransfer(double direction){
//
//        if(direction > 0 && direction <=  11.25){
//
//            return Constant.WIND_N;
//
//        }else if(direction <=33.75){
//
//            return Constant.WIND_NNE;
//
//        }else if(direction <= 56.25){
//
//            return  Constant.WIND_NE ;
//
//        } else if (direction <= 78.75){
//
//            return  Constant.WIND_ENE ;
//
//        } else if (direction <= 101.25){
//
//            return Constant.WIND_E ;
//
//        } else if (direction <= 123.75){
//
//            return Constant.WIND_ESE ;
//
//        }else if (direction <= 146.25){
//
//            return Constant.WIND_SE ;
//
//        } else if (direction <= 168.75){
//
//            return Constant.WIND_SSE;
//
//        } else if (direction <= 191.25){
//
//            return Constant.WIND_S;
//
//        } else if(direction <= 213.75){
//
//            return Constant.WIND_SSW;
//
//        } else if (direction <= 236.25){
//
//            return Constant.WIND_SW;
//
//        } else if (direction <= 258.75) {
//
//            return Constant.WIND_WSW;
//
//        } else if (direction <= 281.25) {
//
//            return Constant.WIND_W;
//
//        } else if (direction <= 303.75) {
//
//            return Constant.WIND_WNW ;
//
//        } else if (direction <= 326.25) {
//
//            return Constant.WIND_NW ;
//
//        } else if (direction <= 348.75) {
//
//            return Constant.WIND_NNW;
//
//        } else if(direction <= 360){
//
//            return Constant.WIND_N;
//
//        }else {
//
//         return "";
//
//        }
//
//    }

	public static   ListObject queryListObject(Object obj,String message){
		ListObject listObject = new ListObject();
		listObject.setData(obj);
		listObject.setMessage(message);
		listObject.setStatus(StatusCode.CODE_SUCCESS);
		return listObject;
	}
	//存储成功
	public static   ListObject saveListObject(Object obj,String message,int status){
		ListObject listObject = new ListObject();
		listObject.setData(obj);
		listObject.setMessage(message);
		listObject.setStatus(status);
		return listObject;
	}
	//失败
	public static ListObject error(String message) {
		ListObject listObject = new ListObject();
		listObject.setMessage(message);
		listObject.setStatus(StatusCode.CODE_ERROR);
		return listObject;
		
	}
	public static AbstractJsonObject queryAbstractJsonObject(String message, int code){
		AbstractJsonObject abstractJsonObject = new AbstractJsonObject();
		abstractJsonObject.setStatus(code);
		abstractJsonObject.setMessage(message);
		return abstractJsonObject;
	}

	public static boolean isNum(String str) {
		return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
	}


	public static int getRandNum(int min, int max) {
		int randNum = min + (int)(Math.random() * ((max - min) + 1));
		return randNum;
	}

	public static String getUUID(){
		return UUID.randomUUID().toString();
	}


	public static Long queryTime(Date startTime,Date endTime){
		Calendar dateOne = Calendar.getInstance();
		Calendar dateTwo = Calendar.getInstance();
		dateOne.setTime(startTime);
		dateTwo.setTime(endTime);
		long timeOne=dateOne.getTimeInMillis();
		long timeTwo=dateTwo.getTimeInMillis();
		long minute=(timeOne-timeTwo)/(1000*60);//转化minute
		return minute;
	}
	/**
	 * 将java.sql.Timestamp对象转化为java.util.Date对象
	 * 
	 * @param ruleOn
	 *            要转化的java.sql.Timestamp对象
	 * @return 转化后的java.util.Date对象
	 */
	public static java.util.Date timeToDate(Time ruleOn) {
	    return ruleOn;
	}
}
